<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2005 The zen-cart developers                           |
// |                                                                      |
// | http://www.zen-cart.com/index.php                                    |
// |                                                                      |
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
// $Id: fix_cache_key.php  2005-05-05  drbyte $
//

/* 
 * This utility is used to reset your "SESSION_WRITE_DIRECTORY" key after switching servers.
 *
 * 1. Make sure the DIR_FS_SQL_CACHE setting is correct in your /includes/configure.php file.
 * 2. Copy this file to the folder where "index.php" exists for your Zen Cart site
 * 3. Run it
 * 4. The cache setting from your configure.php file will be displayed, followed by
 *    the cache setting from your database.
 *    If they match, it will say "No changes required."
 *    If they do not match, it will synchronize the database to match the configure.php setting.
 * 5. VERY IMPORTANT .... WHEN DONE, DELETE THIS FILE FROM YOUR SERVER!!!!!!!!!!!
 *    If you don't delete it (or at least rename it to something very obscure), then you run the
 *    risk of somebody using it to find out information about your server configuration, which
 *    could compromise your site's security. Thus, deleting this file when finished is the best approach.
 */


$force = (isset($_GET['force'])) ? $_GET['force'] : false;
?>
<html><head>
<style type="text/css">
<!--
h1 {
	color: #B56224;
}
h2 {
	color: #D67732;
}
h3 {
	color: #D67630;
}
.red {
	color: #FF0000;
	font-weight: bolder;
}
.green {color: #009900;
	font-weight: bolder;
}
-->
</style>
</head><body>
<?php 
require('includes/application_top.php');

echo '<h1>Cache Settings Synchronization Tool</h1>';
echo '<h2>Recommendations</h2>';
$file_path = __FILE__;
$possible_dir = str_replace(basename(__FILE__),'cache',__FILE__);
if (is_dir($possible_dir)) echo '<strong>Possible appropriate cache directory for your site is:</strong> <code>'. $possible_dir.'</code><br /><br />';

echo '<h2>Actual Information</h2>';
//Read configure.php
  $filename = 'includes/configure.php';
  $zdb_sql_cache ='';
  if (file_exists($filename)) {
    $lines = file($filename);
    foreach($lines as $line) { // read the configure.php file for specific variables
      $def_string=array();
      $def_string=explode("'",$line);
      if (strtoupper($def_string[1]) == 'DIR_FS_SQL_CACHE' ) $zdb_sql_cache .= $def_string[3];
    }
  }
   echo '<strong>Read <tt>DIR_FS_SQL_CACHE</tt> from configure.php:</strong> <tt>'.$zdb_sql_cache.'</tt><br />';
   $sql = "select configuration_value from  " . DB_PREFIX . "configuration where configuration_key = 'SESSION_WRITE_DIRECTORY'";
   $result=$db->Execute($sql);

//read database
   $zdb_sess_write_old = $result->fields['configuration_value'];
   echo '<strong>Found <tt>SESSION_WRITE_DIRECTORY</tt> in database:</strong> <tt>'.$zdb_sess_write_old.'</tt><br />';

if ($zdb_sql_cache == $zdb_sess_write_old && $force!=true) {
   echo '<br /><font color=green><strong>No changes required.</strong><br />Exiting.<br /></font>';
} else {
   echo '<br />Now synchronizing...<br />';
//write database
   $sql = "update " . DB_PREFIX . "configuration set configuration_value = '" . $db->prepare_input(trim($zdb_sql_cache)) . "' where configuration_key = 'SESSION_WRITE_DIRECTORY'";
   $db->Execute($sql);
//re-read database
   $sql = "select configuration_value from  " . DB_PREFIX . "configuration where configuration_key = 'SESSION_WRITE_DIRECTORY'";
   $result=$db->Execute($sql);
   $zdb_sess_write_new = $result->fields['configuration_value'];
   echo '<strong>Database now contains the following <tt>SESSION_WRITE_DIRECTORY</tt>: </strong> <tt>'.$zdb_sess_write_new.'</tt><br />';
   echo '<br />Finished.<br />';
}
echo '<h2><font color=red>Security</h2><strong>PLEASE DELETE THIS FILE FROM YOUR SERVER NOW!!</strong></font><br />Removing this file ('.basename(__FILE__). ') from your server protects you from would-be hackers/predators who might seek to compromise your server.';


?>
</body></html>